package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.user.impl.dao.UserProfileDao;
import com.jiantong.user.model.UserProfile;
import com.jiantong.user.service.UserProfileReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/4/5.
 */
@Slf4j
@Service
@RpcProvider
public class UserProfileReadServiceImpl implements UserProfileReadService {

    private final UserProfileDao userProfileDao;

    @Autowired
    public UserProfileReadServiceImpl(UserProfileDao userProfileDao) {
        this.userProfileDao = userProfileDao;
    }

    @Override
    public Response<UserProfile> findUserProfileById(Long userId) {
        try {
            UserProfile userProfile = userProfileDao.findByUserId(userId);
            if (userProfile == null) {
                log.error("userProfile(userId={}) is not found", userId);
                return Response.fail("userProfile.not.found");
            }
            return Response.ok(userProfile);
        } catch (Exception e) {
            log.error("failed to find userProfile(userId={}), cause:{}", userId, Throwables.getStackTraceAsString(e));
            return Response.fail("userProfile.find.fail");
        }
    }

    @Override
    public Response<List<UserProfile>> findUserIds(List<Long> userIds) {
        try {
            Map<String, Object> map = Maps.newHashMap();
            if (userIds != null && !userIds.isEmpty()) {
                map.put("userIds", userIds);
            }
            List<UserProfile> userProfiles = userProfileDao.findByUserIds(map);
            if (userProfiles == null) {
                log.error("userProfile(userId={}) is not found", userIds);
                return Response.fail("userProfile.not.found");
            }
            return Response.ok(userProfiles);
        } catch (Exception e) {
            log.error("failed to find userProfile(userId={}), cause:{}", userIds, Throwables.getStackTraceAsString(e));
            return Response.fail("userProfile.find.fail");
        }
    }

    /**
     * 条件查询user_id
     *
     * @param criteria
     * @return
     */
    public Response<List<Long>> selectUserIds(Map criteria) {
        try {
            return Response.ok(userProfileDao.selectUserIds(criteria));
        } catch (Exception e) {
            log.error("failed to find userProfile(userId={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("userProfile.find.fail");
        }
    }
    /**
     * 条件查询
     *
     * @param criteria
     * @return
     */
    public Response<List<UserProfile>> selectList(Map criteria) {
        try {
            return Response.ok(userProfileDao.selectList(criteria));
        } catch (Exception e) {
            log.error("failed to find userProfile(userId={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("userProfile.find.fail");
        }
    }


}
